package com.e3.basdoc.employeecls.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.e3.basdoc.employeecls.dao.EmployeeClsDAO;
import com.e3.basdoc.employeecls.entity.EmployeeClsEntity;
import com.e3.basdoc.employeecls.model.EmployeeClsModel;
import com.e3.basdoc.employeecls.model.EmployeeClsQC;
import com.e3.basdoc.employeecls.service.EmployeeClsCacheManager;
import com.e3.basdoc.employeecls.service.EmployeeClsQueryService;
import com.e3.core.base.DAOHelper;
import com.e3.core.base.ServiceException;
import com.e3.core.base.SuperQueryService;

public class EmployeeClsQueryServiceImpl extends SuperQueryService implements EmployeeClsQueryService {
	
	private EmployeeClsDAO goodsClsDAO;
	
	public EmployeeClsDAO getEmployeeClsDAO(){
		if(goodsClsDAO==null){
			goodsClsDAO = DAOHelper.createDAOInstance(EmployeeClsDAO.class);
		}
		return goodsClsDAO;
	}
	
	public List<EmployeeClsModel> getEmployeeClsChildren(EmployeeClsModel model)	throws ServiceException {
		if(model == null){
			return null;
		}
		long id = model.getCorpId();
		List<Long> pIds = new ArrayList<Long>(1);
		pIds.add(id);
		
		List<Long> ids = getEmployeeClsDAO().getChildrenIds(pIds);
		return getEmployeeClsByIds(ids);

	}
	
	public List<EmployeeClsModel> getEmployeeClsByIds(List<Long> ids) throws ServiceException {
		if(ids==null||ids.isEmpty()){
			return null;
		}
		List<EmployeeClsModel> models = new ArrayList<EmployeeClsModel>(ids.size());
		for (Long id : ids) {
			EmployeeClsModel model = getEmployeeClsById(id);
			models.add(model);
		}
		return models;
	}
	
	public EmployeeClsModel getEmployeeClsById(long id) throws ServiceException {
		EmployeeClsModel model = EmployeeClsCacheManager.INSTANCE.get(id);
		if(model!=null){
			return model;
		}
		
		EmployeeClsEntity bankEntity = getEmployeeClsDAO().getById(EmployeeClsEntity.class, id);
		if(bankEntity==null){
			return null;
		}
		EmployeeClsModel rModel = bankEntityToModel(bankEntity);
		long size = getEmployeeClsDAO().getChildrenSize(id);
		if(rModel!=null){
			rModel.setHasChildren(size>0);
			EmployeeClsCacheManager.INSTANCE.put(rModel);
		}
		return rModel;
	}
	
	public List<EmployeeClsModel> getEmployeeClsByCondition(EmployeeClsQC bankQC) throws ServiceException{
		List<Long> ids = getEmployeeClsDAO().getEmployeeClsIdsByCondition(bankQC);
		if(ids!=null&&ids.size()>0){
			return getEmployeeClsByIds(ids);
		}
		return null;
	}

	private EmployeeClsModel bankEntityToModel(EmployeeClsEntity entity) {
		
		if(entity == null){
			return null;
		}
		
		EmployeeClsModel model = new EmployeeClsModel();
		model = ocEntityToModel(model, entity);

		model.setId(entity.getId());
		model.setParentId(entity.getParentId());
		model.setCode(entity.getCode());
		model.setName(entity.getName());
		model.setRemark(entity.getRemark());
				
		return model;

	}

	public List<EmployeeClsModel> getAllEmployeeClses() throws ServiceException {
		return getEmployeeClsDAO().getAllEmployeeClses();
	}
}
